package com.power.using.askapp.application;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.power.using.askapp.store.PersistentCookieStore;
import com.zhy.autolayout.config.AutoLayoutConifg;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.IOException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.JavaNetCookieJar;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by 聪明一只哈 on 2019/1/17.
 * Time: 2019/1/17  16:43
 */
public class MyApplication extends Application {

    public static Context sContext;
    private static MyApplication sApplication;

    //刷新头  下拉刷新框架配置
//    static {
//        //启用矢量图兼容
//        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
//        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
//            @NonNull
//            @Override
//            public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
//                //全局设置主题颜色（优先级第二低，可以覆盖 DefaultRefreshInitializer 的配置，与下面的ClassicsHeader绑定）
//                layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
//
//                return new ClassicsHeader(context).setTimeFormat(new DateFormat() {
//                    @Override
//                    public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
//
//                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                        String time = format.format(date);
//
//                        return toAppendTo.append(time);
//                    }
//
//                    @Override
//                    public Date parse(String source, ParsePosition pos) {
//                        return null;
//                    }
//                });
//            }
//        });
//    }

    @Override
    public void onCreate() {
        super.onCreate();
        sApplication = this;
        sContext = getApplicationContext();

        //使用autolayot
        AutoLayoutConifg.getInstance().useDeviceSize().init(this);
        //初始化okhttp
        initOKHttps();
    }

    private void initOKHttps() {
        CookieHandler cookieHandler = new CookieManager(new PersistentCookieStore(getApplicationContext()),
                CookiePolicy.ACCEPT_ALL);


        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addNetworkInterceptor(new MyApplication.CacheInterceptor())
                .writeTimeout(30000L, TimeUnit.SECONDS)
                .readTimeout(30000L, TimeUnit.SECONDS)
                .connectTimeout(30000L, TimeUnit.SECONDS)
                .addInterceptor(logging)
                .cookieJar(new JavaNetCookieJar(cookieHandler))
                .build();

        OkHttpUtils.initClient(okHttpClient);

    }

    public class CacheInterceptor implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Response response = chain.proceed(request);
            int code = response.code();
            Log.e("显示","code值"+code);
            Response response1 = response.newBuilder()
                    .removeHeader("Pragma")
                    .removeHeader("Cache-Control")
                    //cache for 30 days
                    .header("Cache-Control", "max-age=" + 3600 * 24 * 30)
                    .build();
            return response1;
        }
    }

    public static MyApplication getApplicationIntstance() {
        return sApplication;
    }

}
